﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace BEGDC.CC.Web.Control
{
    [DefaultProperty("Text"),
    ToolboxData("<{0}:UserSearchControl2 runat=server></{0}:UserSearchControl2>")]
    public class UserSearchControl2 : System.Web.UI.WebControls.WebControl, IPostBackDataHandler
    {
        public UserSearchControl2() : base()
		{
			box = new TextBox();
			box.CssClass = "VALUEBOX";
			box.ReadOnly = true;
			empid = new HtmlInputHidden();
			selfid = new HtmlInputHidden();
			//depts = new HtmlInputHidden();
			this.empid.Value = string.Empty;
			this.selfid.Value = string.Empty;
			//this.depts.Value ="";
		}

		private TextBox box;
		private HtmlInputHidden empid;
		private HtmlInputHidden selfid;
		//private HtmlInputHidden depts;

		protected override void Render(HtmlTextWriter output)
		{
			output.AddAttribute(HtmlTextWriterAttribute.Type,"hidden");
			output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
			output.RenderBeginTag(HtmlTextWriterTag.Input);
			output.RenderEndTag();

			this.Controls.Clear();

			HtmlTable table = new HtmlTable();
			table.Border = 0;
			table.CellSpacing = 0;
			table.CellPadding = 0;

			HtmlTableRow row = new HtmlTableRow();

			HtmlTableCell cell1 = new HtmlTableCell();	
			box.ID = this.ID+"_Employee_Name";
			box.Attributes.Add("runat", "server");
			box.Width = System.Web.UI.WebControls.Unit.Pixel(150);
			empid.ID = this.ID+"_Employee_ID";
			cell1.Controls.Add(box);
			cell1.Controls.Add(empid);
			//self
			this.selfid.ID = this.ID+"_Self_ID";
			cell1.Controls.Add(selfid);
			HtmlTableCell cell2 = new HtmlTableCell();
			cell2.InnerHtml = "&nbsp;<img id=\"imgSearchUser\" align=\"middle\" src=\"../../images/Search.gif\" onclick=\"javascript:ShowUserSelectForm_"+this.ClientID+"();\" style=\"cursor:hand\" /> ";
			row.Cells.Add(cell1);
			row.Cells.Add(cell2);
			table.Rows.Add( row );
			this.Controls.Add(table);
			this.RenderChildren(output);
			WriteClientScript(output);
		}

		private void WriteClientScript(HtmlTextWriter output)
		{
			output.WriteLine("<script language=\"javascript\">");
			output.WriteLine("<!--");
			output.WriteLine("function ShowUserSelectForm_"+this.ClientID+"()");
			output.WriteLine("{");
			output.WriteLine("	var name_box = document.getElementById(\""+this.ClientID+"_Employee_Name\");");
			output.WriteLine("	var id_value = document.getElementById(\""+this.ClientID+"_Employee_ID\");");
			if(this.TitleControlName.Length >0)			output.WriteLine("	var title_box = document.getElementById(\""+this.TitleControlName+"\");");
			output.WriteLine("	var selfid_value = document.getElementById(\""+this.ClientID+"_Self_ID\");");

			output.WriteLine("var name = name_box.value;");
			output.WriteLine("var code = window.showModalDialog('../../Workflow/SelectEngineerForm.aspx?Depts="+this.department+"&type="+this.role+"&self=" + this.selfid.Value +" ','','dialogHeight:340px;dialogWidth:370px;dialogTop:px;dialogLeft:px;center:Yes;scroll:yes;resizable:yes;status:no;')");
			
			output.WriteLine("if(code != null && code != '')");
			output.WriteLine("{");
			output.WriteLine("  var temp = code.split(\"|\");");			
			output.WriteLine("  name_box.value = temp[0];");
			output.WriteLine("  id_value.value = temp[1];");
			if(this.TitleControlName.Length >0) output.WriteLine("  title_box.value = temp[2];");
			output.WriteLine("}");
			if (this.is_post_back)
			{
				output.WriteLine("	var theform;");
				output.WriteLine("	if (window.navigator.appName.toLowerCase().indexOf(\"netscape\") > -1) ");
				output.WriteLine("	{");
				output.WriteLine("		theform = document.forms[\"Form1\"];");
				output.WriteLine("	}");
				output.WriteLine("	else ");
				output.WriteLine("	{");
				output.WriteLine("		theform = document.Form1;");
				output.WriteLine("	}");			
				output.WriteLine("	theform.submit();");
			}
			output.WriteLine("}");
			output.WriteLine("//-->");
			output.WriteLine("</script>");
		}

		private string role;
		private string department;

		public string Role
		{
			get
			{
				return this.role;
			}
			set
			{
				this.role = value;
			}
		}

		public string EmployeeName
		{
			get
			{
				return this.box.Text;
			}
			set
			{
				this.box.Text = value;
			}
		}

		public string EmployeeID
		{
			get
			{
				return this.empid.Value;
			}
			set
			{
				this.empid.Value = value.ToString();
			}
		}

		public string SelfID
		{
			get
			{
				return this.selfid.Value;
			}
			set
			{
				this.selfid.Value = value.ToString();
			}
		}

		public string Departments
		{
			get
			{
				return this.department;
			}
			set
			{  
				this.department = value;
			}
		}

		public string CssClassName
		{
			get
			{
				return this.box.CssClass;
			}
			set
			{
				this.box.CssClass = value.ToString();
			}
		}

		public TextBox Box
		{
			get
			{
				return this.box;
			}
		}

		private string titleControlName = "";
		public string TitleControlName
		{
			get{return titleControlName;}
			set{titleControlName = value;}
		}

		private bool is_post_back = false;
		public bool IsPostBack
		{
			get
			{
				return this.is_post_back; 
			}
			set
			{
				this.is_post_back = value;
			}
		}

		protected override object SaveViewState()
		{			
			object[] objArray1 = new object[] { this.box.Text, this.empid.Value, this.selfid.Value,this.titleControlName};
			return objArray1;
		}
	
		protected override void LoadViewState(object savedState)
		{
			if (savedState != null)
			{
				object[] objArray1 = (object[]) savedState;
				if (objArray1[0] != null)
				{
					this.box.Text = (string)objArray1[0];
				}
				if (objArray1[1] != null)
				{
					this.empid.Value = (string)objArray1[1];
				}
				if (objArray1[2] != null)
				{
					this.selfid.Value = (string)objArray1[2];
				}
				if(objArray1[3]!= null)
				{
					this.titleControlName = (string)objArray1[3];
				}
			}
		}

		public event EventHandler OnEmployeeChanged;

		#region IPostBackDataHandler Members

		public void RaisePostDataChangedEvent()
		{
			if (OnEmployeeChanged != null)
				this.OnEmployeeChanged(this, null);
		}

		public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
		{
			try
			{
				this.box.Text = postCollection[this.UniqueID+"_Employee_Name"];
				string emplid = postCollection[this.UniqueID+"_Employee_ID"];
				//string selfid = postCollection[this.UniqueID+"_Self_ID"];
				if (emplid != this.empid.Value)
				{
					this.empid.Value = postCollection[this.UniqueID+"_Employee_ID"];
					return true;	
				}				
			}
			catch
			{
				
			}
			return false;
		}

		#endregion
    }
}
